## ANTES DE USAR
# Para criar data/movies.csv
# import_data("tom_cruise") 
#ou com o ator/atriz que você escolher
import_data("jake_gyllenhaal") 
## Loading required package: rvest
## Loading required package: xml2
## 
## Attaching package: 'rvest'
## The following object is masked from 'package:purrr':
## 
##     pluck
## The following object is masked from 'package:readr':
## 
##     guess_encoding
## Warning in eval(substitute(expr), envir, enclos): NAs introduced by
## coercion
filmes = read_imported_data()
filmes
## # A tibble: 20 x 5
##    avaliacao filme                               papel    bilheteria   ano
##        <int> <chr>                               <chr>         <dbl> <int>
##  1        92 Stronger                            Jeff Ba…        4.2  2017
##  2        68 Life                                David J…       30.2  2017
##  3        73 Nocturnal Animals                   Tony Ha…       10.7  2016
##  4        52 Demolition                          Davis M…        1.7  2016
##  5        73 Everest                             Scott F…       46.6  2015
##  6        59 Southpaw                            "Billy …       42.4  2015
##  7        82 Prisoners                           Detecti…       61    2013
##  8        85 End of Watch                        Taylor …       39.1  2012
##  9        92 Source Code                         Capt. C…       54.7  2011
## 10        49 Love and Other Drugs                Jamie R…       33.3  2010
## 11        35 Prince of Persia: The Sands of Time Dastan         90.8  2010
## 12        64 Brothers                            Tommy C…       28.6  2009
## 13        47 Rendition                           Douglas…        9.7  2007
## 14        90 Zodiac                              Robert …       33    2007
## 15        87 Brokeback Mountain                  Jack Tw…       83    2005
## 16        61 Jarhead                             Anthony…       62.6  2005
## 17        62 Proof                               Hal             7.5  2005
## 18        44 The Day After Tomorrow              Sam Hall      187.   2004
## 19        82 The Good Girl                       Holden         13.8  2002
## 20        86 Lovely & Amazing                    Jordan          4.2  2002

Descrição

filmes %>% 
    ggplot(aes(x = ano, y = bilheteria)) + 
    geom_point(size = 4, color = paleta[1]) 

filmes %>% 
    ggplot(aes(x = bilheteria)) + 
    geom_histogram(binwidth = 15, fill = paleta[2], color = "black") + 
    geom_rug(size = .5) 

filmes %>% 
    ggplot(aes(x = avaliacao)) + 
    geom_histogram(binwidth = 10, boundary = 0, fill = paleta[3], color = "black") + 
    geom_rug(size = .5) 

Estrutura de grupos?

p = filmes %>% 
    ggplot(aes(x = "", y = bilheteria, label = filme)) + 
    geom_jitter(width = .05, alpha = .3, size = 3) + 
    labs(x = "")

ggplotly(p)

Agrupamento hierárquico

agrupamento_h = filmes %>% 
    mutate(nome = paste0(filme, " (av=", avaliacao, ")")) %>% 
    as.data.frame() %>% 
    column_to_rownames("filme") %>% 
    select(avaliacao) %>%
    dist(method = "euclidian") %>% 
    hclust(method = "ward.D")

ggdendrogram(agrupamento_h, rotate = T, size = 2, theme_dendro = F) + 
    labs(y = "Dissimilaridade", x = "", title = "Dendrograma")

get_grupos <- function(agrupamento, num_grupos){
    agrupamento %>% 
        cutree(num_grupos) %>% 
        as.data.frame() %>% 
        mutate(label = rownames(.)) %>% 
        gather(key =  "k", value = "grupo", -label) %>% 
        mutate(grupo = as.character(grupo))
}

atribuicoes = get_grupos(agrupamento_h, num_grupos = 1:6)

atribuicoes = atribuicoes %>% 
    left_join(filmes, by = c("label" = "filme"))

atribuicoes %>% 
    ggplot(aes(x = "Filmes", y = avaliacao, colour = grupo)) + 
    geom_jitter(width = .02, height = 0, size = 1.6, alpha = .6) + 
    facet_wrap(~ paste(k, " grupos")) + 
    scale_color_brewer(palette = "Dark2")

k_escolhido = 3

atribuicoes %>% 
    filter(k == k_escolhido) %>% 
    ggplot(aes(x = reorder(label, avaliacao), y = avaliacao, colour = grupo)) + 
    geom_jitter(width = .02, height = 0, size = 3, alpha = .6) + 
    facet_wrap(~ paste(k, " grupos")) + 
    scale_color_brewer(palette = "Dark2") + 
    labs(x = "", y = "Avaliação RT") + 
    coord_flip() 

Com duas dimensões

agrupamento_h_2d = filmes %>% 
    mutate(bilheteria = log10(bilheteria)) %>%
    mutate_at(vars(avaliacao, bilheteria), funs(scale)) %>%
    column_to_rownames("filme") %>%
    select(avaliacao, bilheteria) %>%
    dist(method = "euclidean") %>% 
    hclust(method = "ward.D")    
## Warning: Setting row names on a tibble is deprecated.
ggdendrogram(agrupamento_h_2d, rotate = TRUE, theme_dendro = F)

filmes %>% 
    ggplot(aes(bilheteria, ..density..)) + 
    geom_histogram(bins=5,fill = paleta[3], color = "black") +
    scale_x_log10()

agrupamento_h_2d = filmes %>%
   mutate(bilheteria = log10(bilheteria)) %>%
   mutate_at(vars("avaliacao", "bilheteria"), funs(scale)) %>%
   column_to_rownames("filme") %>%
   select("avaliacao", "bilheteria") %>%
   dist(method = "euclidean") %>%
   hclust(method = "centroid")
## Warning: Setting row names on a tibble is deprecated.
ggdendrogram(agrupamento_h_2d, rotate = TRUE, theme_dendro = F)

filmes2 = filmes %>% mutate(bilheteria = log10(bilheteria))
plota_hclusts_2d(agrupamento_h_2d,
                filmes2,
                c("avaliacao", "bilheteria"),
                linkage_method = "centroid", ks = 1:6) +
   scale_y_log10() +
   scale_color_brewer(palette = "Dark2")
## Scale for 'colour' is already present. Adding another scale for
## 'colour', which will replace the existing scale.